var express = require('express');
var router = express.Router();
var crypto = require('crypto');
var User = require('../models/user.js');
var Post = require('../models/post.js');

/* GET home page. */
router.get('/', function (req, res) {
  Post.getAll(null, function(err, posts){
  	if(err){
  		posts = [];
  	}
  	res.render('index', { 
	  	title: '首页' ,
	  	user: req.session.user,
	  	posts: posts,
	  	success: req.flash('success').toString(),
	  	error: req.flash('success').toString(),
	  	current: req.path
	});
  })
  
});

router.get('/login', checkNotLogin);
router.get('/login', function (req, res) {
  res.render('login', { 
  	title: '登录',
  	user: req.session.user,
  	success: req.flash('success').toString(),
  	error: req.flash('error').toString(),
  	current: req.path
  });
});

router.post('/login', checkNotLogin);
router.post('/login', function (req, res){
  var md5 = crypto.createHash('md5');
  var password = md5.update(req.body.password).digest('hex');
  User.get(req.body.name,function(err, user){
  	if(!user){
  		req.flash('error','用户不存在');
  		return res.redirect('/login');
  	}
  	if(user.password != password){
  		req.flash('error','密码错误');
  		return res.redirect('/login');
  	}
  	req.session.user = user;
  	req.flash('success','登录成功');
  	res.redirect('/');
  });
})

router.get('/register', checkNotLogin);
router.get('/register', function (req, res) {
  res.render('register', { 
  	title: '注册' ,
  	user: req.session.user,
  	success: req.flash('success').toString(),
  	error: req.flash('error').toString(),
  	current: req.path
  });
});

router.post('/register', checkNotLogin);
router.post('/register', function (req, res) {
	var name = req.body.name,
		password = req.body.password,
		password_repeat = req.body['password-repeat'];
	if(password != password_repeat){
		req.flash('error','两次输入的密码不一致');
		return res.redirect('/register');
	}
	var md5 = crypto.createHash('md5');
	password = md5.update(password).digest('hex');
	var newUser = new User({
		name:name,
		password:password,
		email:req.body.email
	});
	User.get(newUser.name, function (err, user){
		if(user){
			req.flash('error','用户已存在!');
			return res.redirect('/register');
		}
		newUser.save(function (err,user){
			if(err){
				req.flash('error',err);
				return res.redirect('/register');
			}
			req.session.user = user;
			req.flash('success','注册成功!');
			res.redirect('/');
		});
	});
});

router.get('/post', checkLogin);
router.get('/post', function (req, res) {
  res.render('post', { 
  	title: '发表' ,
  	user: req.session.user,
  	success: req.flash('success').toString(),
  	error: req.flash('error').toString(),
  	current: req.path
  });
});
router.post('/post', checkLogin);
router.post('/post', function (req, res) {
	var user = req.session.user;
	var post = new Post(
		user.name,
		req.body.title,
		req.body.post,
		req.body.abstracts,
		req.body.keywords,
		req.body.category==""?0:parseInt(req.body.category),
		req.body.original=="on"?true:false,
		req.body.visible=="on"?true:false,
		req.body.allowcomment=="on"?true:false,
		req.body.top=="on"?true:false
		);
	post.save(function (err){
		if(err){
			req.flash('error',err);
			return res.redirect('/');
		}
		req.flash('success','发布成功！');
		res.redirect('/');
	})
})

router.get('/logout', checkLogin);
router.get('/logout', function (req, res) {
  req.session.user = null;
  req.flash('success', '登出成功!');
  res.redirect('/');//登出成功后跳转到主页
});

router.get('/u/:name',function (req, res){
	User.get(req.params.name, function (err,user){
		if(!user){
			req.flash('error','用户不存在');
			return res.redirect('/');
		}

		Post.getAll(user.name,function (err,posts){
			if(err){
				req.flash('error',err);
				return res.redirect('/');
			}
			res.render('user',{
				title: user.name,
				posts: posts,
				user: req.session.user,
				success: req.flash('success').toString(),
				error: req.flash('error').toString()
			});
		});
	});
});

router.get('/u/:name/:day/:title',function (req, res){
	Post.getOne(req.params.name, req.params.day, req.params.title, function(err,post){
		if(err){
			req.flash('error',err);
			return res.redirect('/');
		}
		res.render('article', {
			title:req.params.title,
			post:post,
			user:req.session.user,
			success: req.flash('success').toString(),
			error: req.flash('error').toString()
		})
	})
})

module.exports = router;

function checkLogin (req, res, next){
	if(!req.session.user){
		req.flash('error','未登录');
		res.redirect('/login');
	}
	next();
}
function checkNotLogin (req, res, next){
	if(req.session.user){
		req.flash('error','已登录');
		res.redirect('back');
	}
	next();
}